package com.xjrsoft.module.liteflow.controller;

import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjrsoft.common.constant.GlobalConstant;
import com.xjrsoft.common.model.result.R;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.utils.RedisUtil;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.demo.dto.ChainPageDto;
import com.xjrsoft.module.liteflow.dto.AddLiteflowCodeDto;
import com.xjrsoft.module.liteflow.dto.UpdateLiteflowCodeDto;
import com.xjrsoft.module.liteflow.entity.LiteflowCode;
import com.xjrsoft.module.liteflow.service.ILiteflowCodeService;
import com.xjrsoft.module.liteflow.vo.LiteflowCodePageVo;
import com.xjrsoft.module.organization.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javassist.CannotCompileException;
import javassist.NotFoundException;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 规则代码表 前端控制器
 * </p>
 *
 * @author hnyyzy
 * @since 2024-05-17
 */
@RestController
@RequestMapping(GlobalConstant.LITEFLOW_MODULE_PREFIX + "/code")
@Api(value = GlobalConstant.LITEFLOW_MODULE_PREFIX + "/code", tags = "规则代码模块")
@AllArgsConstructor
public class LiteflowCodeController {

    private ILiteflowCodeService liteflowCodeService;

    private RedisUtil redisUtil;

    @GetMapping(value = "/page")
    @ApiOperation(value = "规则代码(分页)")
    public R page(@Valid ChainPageDto dto){
        LambdaQueryWrapper<LiteflowCode> queryWrapper = Wrappers.lambdaQuery(LiteflowCode.class)
                .like(StrUtil.isNotBlank(dto.getKeyword()), LiteflowCode::getMethodName, dto.getKeyword())
                .or()
                .like(StrUtil.isNotBlank(dto.getKeyword()), LiteflowCode::getCodeNumber, dto.getKeyword())
                .orderByDesc(LiteflowCode::getCreateDate)
                .select(LiteflowCode.class, x -> VoToColumnUtil.fieldsToColumns(LiteflowCodePageVo.class).contains(x.getProperty()));
        IPage<LiteflowCode> page = liteflowCodeService.page(ConventPage.getPage(dto), queryWrapper);
        PageOutput<LiteflowCodePageVo> pageOutput = ConventPage.getPageOutput(page, LiteflowCodePageVo.class);
        List<LiteflowCodePageVo> list = new ArrayList<>(pageOutput.getList().size());
        if (pageOutput.getList().size() > 0){
            List<User> allUser = redisUtil.get(GlobalConstant.USER_CACHE_KEY, new TypeReference<List<User>>() {
            });
            for (LiteflowCodePageVo record : pageOutput.getList()) {
                User createUser = allUser.stream().filter(x -> x.getId().equals(record.getCreateUserId())).findFirst().orElse(new User());
                if (StrUtil.isNotBlank(createUser.getName())){
                    record.setCreateUserName(createUser.getName());
                }
                User modifyUser = allUser.stream().filter(x -> x.getId().equals(record.getModifyUserId())).findFirst().orElse(new User());
                if (StrUtil.isNotBlank(modifyUser.getName())){
                    record.setModifyUserName(modifyUser.getName());
                }
                list.add(record);
            }
        }
        pageOutput.setList(list);
        return R.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value = "规则代码详情")
    public R info(@RequestParam Long id) {
        return R.ok(liteflowCodeService.getById(id));
    }

    @PostMapping
    @ApiOperation(value = "新增规则代码")
    public R add(@Valid @RequestBody AddLiteflowCodeDto dto) throws NoSuchMethodException, InstantiationException, IllegalAccessException {
        return R.ok(liteflowCodeService.addCode(dto));
    }

    @PutMapping
    @ApiOperation(value = "修改规则代码")
    public R update(@Valid @RequestBody UpdateLiteflowCodeDto dto) throws NoSuchMethodException, InstantiationException, IllegalAccessException {
        return R.ok(liteflowCodeService.updateCode(dto));
    }

    @DeleteMapping
    @ApiOperation(value = "删除")
    public R delete(@Valid @RequestBody List<Long> ids) {
        return R.ok(liteflowCodeService.deleteCode(ids));
    }

}
